use crate::commands::list::List;
use crate::error::TodoError;
use crate::storage::Storage;
use crate::ExecuteCommand;
use anyhow::Result;
use chrono::NaiveDate;
use clap::Parser;

#[derive(Parser, Debug)]
pub struct Delete {
    /// 要删除的任务的 ID（需要 id 或 date，但不能同时提供两者）
    #[arg(required_unless_present = "date")]
    pub id: Option<u32>,

    /// 要从中删除任务的日期（id或日期是必需的，但不能同时提供两者）
    #[arg(short,long,value_parser=crate::commands::parse_date)]
    pub date: Option<NaiveDate>,

    /// 修改后展示今天的待办
    #[arg(short = 'l', long = "list")]
    pub show_list: bool,
}

impl ExecuteCommand for Delete {
    fn execute_command(&self, storage: &impl Storage) -> Result<()> {
        match (self.id, self.date) {
            (Some(id), None) => storage.delete_by_id(id)?,
            (None, Some(date)) => storage.delete_by_date(&date)?,
            _ => return Err(TodoError::ParamError("请指定id或date".to_string()).into()),
        }

        if self.show_list {
            let list = List {
                days: None,
                status: None,
            };
            list.execute_command(storage)?;
        }

        Ok(())
    }
}